package scau.erps.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.service.IdEntityService;

import scau.erps.app.Erps;
import scau.erps.entity.Bidding;
import scau.erps.entity.Sand;
import scau.erps.service.BiddingService;

/**
 * 竞价服务实现类
 * @author DCJ
 *
 */
public class BiddingServiceImpl extends IdEntityService<Bidding> implements BiddingService{

	@Override
	public void save(Bidding bidding) {
		this.dao().insert(bidding);
		Sand sand = Erps.getSand();
		int cash = sand.getCash();
		int advertisingFee = sand.getAdvertisingFee();
		sand.setCash(cash-1);
		sand.setAdvertisingFee(advertisingFee+1);
		this.dao().update(sand);
	}

	@Override
	public List<Bidding> getAllBiddings() {
		return this.query(null, null);
	}

	@Override
	public List<Bidding> getBiddings(int team_Id,String year, String status) {
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		SqlExpressionGroup e3 = null;
		if(team_Id != 0){
			e1 = Cnd.exps("team_Id", "=", team_Id);			
		}
		if(year != null){
			e2 = Cnd.exps("year", "=", year);
		}
		if(status != null){
			e3 = Cnd.exps("status", "=", status);
		}
		return this.query(Cnd.where(e1).and(e2).and(e3), null);
	}
	
	/**
	 * 根据小组号和订单号找竞价
	 * @param team_Id
	 * @param order_Id
	 * @return
	 */
	@Override
	public List<Bidding> getBiddings(int team_Id, int order_Id){
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(team_Id != 0){
			e1 = Cnd.exps("team_Id", "=", team_Id);
		}
		if(order_Id != 0){
			e2 = Cnd.exps("order_Id", "=", order_Id);
		}
		return this.query(Cnd.where(e1).and(e2), null);
		
	}
	
	/**
	 * 找到对指定订单进行报价的竞价
	 * @param year
	 * @param order_Id
	 * @return
	 */
	@Override
	public List<Bidding> getBiddings(String year,int order_Id){
		SqlExpressionGroup e1 = null;
		SqlExpressionGroup e2 = null;
		if(year != null){
			e1 = Cnd.exps("year", "=", year);
		}
		if(order_Id != 0){
			e1 = Cnd.exps("order_Id", "=", order_Id);
		}
		return this.query(Cnd.where(e1).and(e2), null);
	}

	@Override
	public void delete(Bidding bidding) {
		this.dao().delete(bidding);		
	}

	@Override
	public Bidding getBiddingById(int id) {
		return this.fetch(id);
	}

	/* 订单匹配
	 * @see scau.erps.service.BiddingService#biddingMatching(java.lang.String)
	 */
	@Override
	public void biddingMatching(String year) {
		List<Bidding> list = getBiddings(year,0); //找到year年竞价数据
		int n = list.size();		
		int[] orderIds = new int[n];  
		Iterator<Bidding> iter = list.iterator();
		for (int i = 0; i < n; i++) {
			Bidding bidding = iter.next();
			orderIds[i] = bidding.getOrder_Id();   //找到被竞价的所有订单号
		}
		HashSet<Integer> set=new HashSet<Integer>(); //过滤掉重复的订单号
		for(int i = 0; i < orderIds.length; i++){
			set.add(orderIds[i]);
		}
		for (Integer a : set) {   //对有人投标的订单进行遍历
			List<Bidding> list1 = new ArrayList<Bidding>();
			list1 = getBiddings(year,a);
			Collections.sort(list1, new Comparator<Bidding>(){
				@Override
				public int compare(Bidding o1, Bidding o2) {
					int i = o1.getMyprice().compareTo(o2.getMyprice()); //第一轮比较竞价，低的拍前；
					if(i==0){
						int j = o2.getScore().compareTo(o1.getScore()); //如竞价相同，第二轮比较分数，高的拍前；
						if(j==0){
							return o1.getDatetime().compareTo(o2.getDatetime()); //如分数相同，最早竞标的拍前；
						}
						return j;
					}
					return i;					
					}
				});
			Bidding bidding = list1.get(0);
			bidding.setStatus("竞价成功");
			this.dao().update(bidding);
		}	
	}
}
